package com.th.supcom.pds.impl.service.internal.phamapply;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.carefx.fc.framework.web.gwt.util.FcUtils;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.InpPhamApplyPatInfo;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailVO;
import com.th.supcom.pds.domain.vo.InpPhamDispenseDetailVO;
import com.th.supcom.pds.impl.common.enums.InpDispenseIndicator;
import com.th.supcom.pds.impl.common.enums.InpWithDrawType;

public final class PhamDispenseConvertUtils
{
    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PhamDispenseConvertUtils.class);

    private PhamDispenseConvertUtils ()
    {

    }

    /**
     * 将药品执行明细按照患者分组
     * 
     * @param detailList 执行明细List
     * @param paivisitIdList 患者住院流水号List
     * @return
     */
    public static Map <Long, List <InpPhamDispenseDetailVO>> convertListToMapByPaivisitID (List <InpPhamDispenseDetailVO> detailList,
                                                                                           List <Long> paivisitIdList)
    {
        Map <Long, List <InpPhamDispenseDetailVO>> map = new HashMap <Long, List <InpPhamDispenseDetailVO>> ();
        for (InpPhamDispenseDetailVO inpPhamDispenseDetailVO : detailList)
        {
            List <InpPhamDispenseDetailVO> temp = null;

            if (map.containsKey (inpPhamDispenseDetailVO.getPaiVisitId ()))
            {
                temp = map.get (inpPhamDispenseDetailVO.getPaiVisitId ());
                temp.add (inpPhamDispenseDetailVO);
                continue;
            }

            paivisitIdList.add (inpPhamDispenseDetailVO.getPaiVisitId ());
            temp = new ArrayList <InpPhamDispenseDetailVO> ();
            temp.add (inpPhamDispenseDetailVO);
            map.put (inpPhamDispenseDetailVO.getPaiVisitId (), temp);

        }
        return map;
    }

    /**
     * 将药品执行明细按照患者分组
     * 
     * @param detailList 执行明细List
     * @param paivisitIdList 患者住院流水号List
     * @return
     */
    public static Map <Long, List <InpPhamDispenseDetailVO>> convertListToMapByPaivisitID (List <InpPhamDispenseDetailVO> detailList)
    {
        Map <Long, List <InpPhamDispenseDetailVO>> map = new HashMap <Long, List <InpPhamDispenseDetailVO>> ();
        for (InpPhamDispenseDetailVO inpPhamDispenseDetailVO : detailList)
        {
            List <InpPhamDispenseDetailVO> temp = null;

            if (map.containsKey (inpPhamDispenseDetailVO.getPaiVisitId ()))
            {
                temp = map.get (inpPhamDispenseDetailVO.getPaiVisitId ());
            }
            else
            {
                temp = new ArrayList <InpPhamDispenseDetailVO> ();
            }
            temp.add (inpPhamDispenseDetailVO);
            map.put (inpPhamDispenseDetailVO.getPaiVisitId (), temp);
        }
        return map;
    }

    /**
     * 将按照患者分组的药品执行明细转换为List
     * 
     * @param detailMap
     * @return
     */
    public static List <InpPhamDispenseDetailVO> convertMapToListByPaivisitID (Map <Long, List <InpPhamDispenseDetailVO>> detailMap)
    {
        List <InpPhamDispenseDetailVO> result = new ArrayList <InpPhamDispenseDetailVO> ();
        // 遍历Map
        Set <Map.Entry <Long, List <InpPhamDispenseDetailVO>>> set = detailMap.entrySet ();
        for (Iterator <Map.Entry <Long, List <InpPhamDispenseDetailVO>>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <Long, List <InpPhamDispenseDetailVO>> entry = (Map.Entry <Long, List <InpPhamDispenseDetailVO>>) it.next ();
            result.addAll (entry.getValue ());
        }
        return result;
    }

    /**
     * 将药品执行明细按照药品标准码分类
     * 
     * @param detailList
     * @return
     */
    public static Map <String, List <InpPhamDispenseDetailVO>> convertListToMapByPhamStdCode (List <InpPhamDispenseDetailVO> detailList,
                                                                                              List <String> phamStdCodeList)
    {
        Map <String, List <InpPhamDispenseDetailVO>> map = new HashMap <String, List <InpPhamDispenseDetailVO>> ();
        for (InpPhamDispenseDetailVO inpPhamDispenseDetailVO : detailList)
        {
            List <InpPhamDispenseDetailVO> temp = null;

            if (map.containsKey (inpPhamDispenseDetailVO.getPhamStdCode ()))
            {
                temp = map.get (inpPhamDispenseDetailVO.getPhamStdCode ());
            }
            else
            {
                phamStdCodeList.add (inpPhamDispenseDetailVO.getPhamStdCode ());
                temp = new ArrayList <InpPhamDispenseDetailVO> ();
            }
            temp.add (inpPhamDispenseDetailVO);
            map.put (inpPhamDispenseDetailVO.getPhamStdCode (), temp);
        }
        return map;
    }

    /**
     * 将药品执行明细按照药品标准码分类
     * 
     * @param detailList
     * @return
     */
    public static Map <String, List <InpPhamDispenseDetailVO>> convertListToMapByPhamStdCode (List <InpPhamDispenseDetailVO> detailList)
    {
        Map <String, List <InpPhamDispenseDetailVO>> map = new HashMap <String, List <InpPhamDispenseDetailVO>> ();
        for (InpPhamDispenseDetailVO InpPhamDispenseDetailVO : detailList)
        {
            List <InpPhamDispenseDetailVO> temp = null;

            if (map.containsKey (InpPhamDispenseDetailVO.getPhamStdCode ()))
            {
                temp = map.get (InpPhamDispenseDetailVO.getPhamStdCode ());
            }
            else
            {
                temp = new ArrayList <InpPhamDispenseDetailVO> ();
            }
            temp.add (InpPhamDispenseDetailVO);
            map.put (InpPhamDispenseDetailVO.getPhamStdCode (), temp);
        }
        return map;
    }

    /**
     * 将药品执行明细按照请领明细主键分类
     * 
     * @param detailList
     * @return
     */
    public static Map <Long, InpPhamDispenseDetailVO> convertListToMapByApplyId (List <InpPhamDispenseDetailVO> detailList)
    {
        Map <Long, InpPhamDispenseDetailVO> map = new HashMap <Long, InpPhamDispenseDetailVO> ();
        for (InpPhamDispenseDetailVO InpPhamDispenseDetailVO : detailList)
        {
            map.put (InpPhamDispenseDetailVO.getInpPhamApplyDetailId (), InpPhamDispenseDetailVO);
        }
        return map;
    }

    /**
     * 将按照药品标准码分类的药品执行明细组合为List
     * 
     * @param detailMap
     * @return
     */
    public static List <InpPhamDispenseDetailVO> convertMapToListByPhamStdCode (Map <String, List <InpPhamDispenseDetailVO>> detailMap)
    {
        List <InpPhamDispenseDetailVO> result = new ArrayList <InpPhamDispenseDetailVO> ();
        // 遍历Map
        Set <Map.Entry <String, List <InpPhamDispenseDetailVO>>> set = detailMap.entrySet ();
        for (Iterator <Map.Entry <String, List <InpPhamDispenseDetailVO>>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <String, List <InpPhamDispenseDetailVO>> entry = (Map.Entry <String, List <InpPhamDispenseDetailVO>>) it.next ();
            result.addAll (entry.getValue ());
        }
        return result;
    }

    /**
     * 将患者住院信息List转换成Map
     * 
     * @param patInfos
     * @return map, key=paiVisitId, value=list
     */
    public static Map <Long, List <InpPhamApplyPatInfo>> convertPatInfoListToMapByPaivisitId (List <InpPhamApplyPatInfo> patInfos)
    {
        Map <Long, List <InpPhamApplyPatInfo>> map = new HashMap <Long, List <InpPhamApplyPatInfo>> ();

        for (InpPhamApplyPatInfo patInfo : patInfos)
        {
            List <InpPhamApplyPatInfo> list = null;
            if (map.containsKey (patInfo.getPaiVisitId ()))
            {
                list = map.get (patInfo.getPaiVisitId ());

            }
            else
            {
                list = new ArrayList <InpPhamApplyPatInfo> ();
            }
            list.add (patInfo);
            map.put (patInfo.getPaiVisitId (), list);
        }
        return map;
    }

    /**
     * 将请领明细VO转化为POJO
     * 
     * @param applyDetailList
     * @return
     */
    public static <T, X> List <X> convertVoToAny (List <T> VO, Class <X> clazz)
    {
        Mapper mapper = new DozerBeanMapper ();
        List <X> result = new ArrayList <X> ();
        for (T vo : VO)
        {
            result.add (mapper.map (vo, clazz));
        }
        return result;
    }

    /**
     * 通过请领明细生成摆药执行明细
     * 
     * @param apply 请领
     * @param operator 操作人
     * @param dispensary 摆药药房
     * @return
     */
    public static List <InpPhamDispenseDetailVO> convertApplyToDispenseDetail (List <InpPhamApplyDetailVO> applyList,
                                                                               String operator, String dispensary)
    {
        List <InpPhamDispenseDetailVO> dispenselist = new ArrayList <InpPhamDispenseDetailVO> ();

        for (InpPhamApplyDetailVO apply : applyList)
        {
            InpPhamDispenseDetailVO dispense = new InpPhamDispenseDetailVO ();
            dispense.setApplyNo (apply.getApplyNo ());
            dispense.setInpPhamApplyDetailId (apply.getInpPhamApplyDetailId ());
            dispense.setOrderNo (apply.getOrderNo ());
            dispense.setOrderSubNo (apply.getOrderSubNo ());
            dispense.setPaiVisitId (apply.getPaiVisitId ());
            dispense.setNurseUnit (apply.getNurseUnit ());
            dispense.setWard (apply.getWard ());
            dispense.setBedNo (apply.getBedNo ());
            dispense.setPhamStdCode (apply.getPhamStdCode ());
            dispense.setPhamSpec (apply.getPhamSpec ());
            dispense.setDosage (apply.getDosage ());
            dispense.setPhamUnit (apply.getPhamUnit ());
            dispense.setPhamName (apply.getPhamName ());
            dispense.setScheduleCount (apply.getScheduleCount ());
            dispense.setApplyAmount (apply.getApplyAmount ());
            if (InpWithDrawType.ReturnWithDrug.getEnumCode ().equals (apply.getWithdrawType ()))
            {
                dispense.setDispenseAmount (0);
            }
            else
            {
                dispense.setDispenseAmount (apply.getApplyAmount ());
            }
            dispense.setDispenseUnit (apply.getApplyUnit ());
            dispense.setFactor (apply.getFactor ());
            dispense.setPerformDispensary (dispensary);
            dispense.setCosts (apply.getCosts ());
            dispense.setCharges (apply.getCharges ());
            dispense.setChargeIndicator (apply.getChargeIndicator ());
            dispense.setDispenseIndicator (apply.getDispenseIndicator ());
            dispense.setDispenseBy (operator);
            dispense.setDispenseDatetime (FcUtils.getCurrentServerDateTimeForServer ());
            dispense.setWithdrawApplyNo (apply.getWithdrawApplyNo ());
            dispense.setWithdrawType (apply.getWithdrawType ());
            dispense.setDestination (apply.getDestination ());
            dispenselist.add (dispense);
        }

        return dispenselist;
    }

    /**
     * 通过请领明细生成摆药执行明细
     * 
     * @param apply 请领
     * @param operator 操作人
     * @param dispensary 摆药药房
     * @return
     */
    public static InpPhamDispenseDetailVO convertApplyToDispenseDetail (InpPhamApplyDetailVO apply, String operator,
                                                                        String dispensary)
    {
        InpPhamDispenseDetailVO dispense = new InpPhamDispenseDetailVO ();
        dispense.setApplyNo (apply.getApplyNo ());
        dispense.setInpPhamApplyDetailId (apply.getInpPhamApplyDetailId ());
        dispense.setOrderNo (apply.getOrderNo ());
        dispense.setOrderSubNo (apply.getOrderSubNo ());
        dispense.setPaiVisitId (apply.getPaiVisitId ());
        dispense.setNurseUnit (apply.getNurseUnit ());
        dispense.setWard (apply.getWard ());
        dispense.setBedNo (apply.getBedNo ());
        dispense.setPhamStdCode (apply.getPhamStdCode ());
        dispense.setPhamSpec (apply.getPhamSpec ());
        dispense.setDosage (apply.getDosage ());
        dispense.setPhamUnit (apply.getPhamUnit ());
        dispense.setPhamName (apply.getPhamName ());
        dispense.setScheduleCount (apply.getScheduleCount ());
        dispense.setApplyAmount (apply.getApplyAmount ());
        dispense.setDispenseAmount (apply.getApplyAmount ());
        dispense.setDispenseUnit (apply.getApplyUnit ());
        dispense.setFactor (apply.getFactor ());
        dispense.setPerformDispensary (dispensary);
        dispense.setCosts (apply.getCosts ());
        dispense.setCharges (apply.getCharges ());
        dispense.setChargeIndicator (apply.getChargeIndicator ());
        dispense.setDispenseIndicator (InpDispenseIndicator.Sorted.getEnumCode ());
        dispense.setDispenseBy (operator);
        dispense.setDispenseDatetime (FcUtils.getCurrentServerDateTimeForServer ());
        dispense.setWithdrawApplyNo (apply.getWithdrawApplyNo ());
        dispense.setWithdrawType (apply.getWithdrawType ());
        dispense.setDestination (apply.getDestination ());
        return dispense;
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
